/*
 * @Author: Jiraiya
 * @Date: 2020-05-07 12:05:25
 * @LastEditors: Jiraiya
 * @LastEditTime: 2020-05-07 15:41:48
 * @Description: 
 */
const utils = require('./utils');
const LOGGER_SYMBOL = Symbol('bee:logger');
const bunyan = require('bunyan');
const fse = require('fs-extra');

const bee = {
    utils,
    systemConfig: {},
    plugins: {},
    middlewareMap: new Map(),
    serverMap: new Map(),
    /**
     * @description: 获取系统配置
     * @param {type} 
     * @return: 
     */    
    getSystemConfig(key){
        return this.systemConfig[key];
    },

    /**
     * @description: 获取其他配置
     * @param {type} 
     * @return: 
     */
    getPlugin(key){
        return this.plugins[key];
    },
}

Object.defineProperty(global, 'bee', {
    value: bee,
    writable: false
});

const BeeApp = require('./BeeApp')

Object.defineProperty(bee, 'BeeApp', {
    value: BeeApp,
    writable: false
});

Object.defineProperty(bee, 'logger', {
    get(){
        if(! this[LOGGER_SYMBOL]){
            const conf = this.getSystemConfig('log');
            const streams = conf.streams || [];
            streams.forEach(({path}) => {
                if(path){
                    fse.ensureFileSync(path);
                }
            });
            this[LOGGER_SYMBOL] = bunyan.createLogger(conf);
        }
        return this[LOGGER_SYMBOL];
    }
});

module.exports = bee;